/*
 * @(#)MaintStaffRecordAction.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.staff.web;

import java.sql.Connection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.SystemFunctionConstant;
import com.dcivision.framework.SystemParameterConstant;
import com.dcivision.framework.SystemParameterFactory;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.UserInfoFactory;
import com.dcivision.framework.Utility;
import com.dcivision.framework.web.AbstractActionForm;
import com.dcivision.framework.web.AbstractMaintAction;
import com.dcivision.staff.bean.StaffHierarchy;
import com.dcivision.staff.dao.StaffHierarchyDAObject;
import com.dcivision.staff.dao.StaffRecordDAObject;
import com.dcivision.user.bean.UserRecord;
import com.dcivision.user.dao.UserRecordDAObject;

/**
  MaintStaffRecordAction.java

  This class is for maint staff record.

    @author          Wong Yam Lee
    @company         DCIVision Limited
    @creation date   28/07/2003
    @version         $Revision: 1.16 $
*/

public class MaintStaffRecordAction extends AbstractMaintAction {

  public static final String REVISION = "$Revision: 1.16 $";

  public MaintStaffRecordAction() {
    super();
  }

  /**
   * getMajorDAOClassName
   *
   * @return  The class name of the major DAObject will be used in this action.
   */
  public String getMajorDAOClassName() {
    return("com.dcivision.staff.dao.StaffRecordDAObject");
  }

  /**
   * getFunctionCode
   *
   * @return  The corresponding system function code of action.
   */
  public String getFunctionCode() {
    return(SystemFunctionConstant.STAFF_RECORD);
  }

  /**
   *  Method execute() - get all list data with different level no and call execute() of AbstractMaintAction
   *
   *  @param      mapping               ActionMapping from struts
   *  @param      form                  ActionForm from struts
   *  @param      request               HttpServletReuqest
   *  @param      respond               HttpServletRespond
   *  @return     ActionForward         Return the action forward object for struts
   *  @throws     ServletException      Throws ServletException
   */
  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException {
    SessionContainer sessionCon = this.getSessionContainer(request);
    Connection conn = this.getConnection(request);
    StaffHierarchyDAObject dao = new StaffHierarchyDAObject(sessionCon, conn);
    final int MAX_LVL = Integer.parseInt(SystemParameterFactory.getSystemParameter(SystemParameterConstant.STAFF_HIERARCHY_LEVEL));

    for ( int i=1; i<=MAX_LVL; ++i ) {
      request.setAttribute("staffList" + i, dao.getListByLevel(new Integer(i)));
    }
    
    MaintStaffRecordForm staffForm = (MaintStaffRecordForm)form;
    String navMode = staffForm.getNavMode();
    Integer levelNO = TextUtility.parseIntegerObj(staffForm.getlevelNo());
    Integer parentID = TextUtility.parseIntegerObj(staffForm.getParentID());
    StaffHierarchy staffBean = null;
    if (!Utility.isEmpty(levelNO) && parentID.intValue() > 0){
      for (int i=0;i<(levelNO.intValue()-1);i++){
        staffBean = (StaffHierarchy)dao.getObjectByID(parentID);
        parentID = staffBean.getParentID();
        switch (staffBean.getLevelNo().intValue()){
          case 1 : staffForm.setLevel1ID((staffBean.getID()).toString());break;
          case 2 : staffForm.setLevel2ID((staffBean.getID()).toString());break;
          case 3 : staffForm.setLevel3ID((staffBean.getID()).toString());break;
          case 4 : staffForm.setLevel4ID((staffBean.getID()).toString());break;
          case 5 : staffForm.setLevel5ID((staffBean.getID()).toString());break;
          case 6 : staffForm.setLevel6ID((staffBean.getID()).toString());break;
          case 7 : staffForm.setLevel7ID((staffBean.getID()).toString());break;
          case 8 : staffForm.setLevel8ID((staffBean.getID()).toString());break;
          case 9 : staffForm.setLevel9ID((staffBean.getID()).toString());break;
          case 10 : staffForm.setLevel10ID((staffBean.getID()).toString());break;
        }
      }
    }
    
    if (GlobalConstant.NAV_MODE_REFRESH.equals(navMode)) {
      log.debug("refreshing ...");
      staffForm.setNavMode("");
      if (isError(request)) {
        staffForm.setNavMode(GlobalConstant.NAV_MODE_EDIT);
        return mapping.findForward(GlobalConstant.NAV_MODE_EDIT);
      } else {
    	//Bill add for EIP-1968
    	request.setAttribute(GlobalConstant.FUNCTION_CODE_KEY, this.getFunctionCode());
        return mapping.findForward(GlobalConstant.NAV_MODE_REFRESH);
      }
    }
    return(super.execute(mapping, form, request, response));
  }

  public void updateRecord(ActionMapping mapping, AbstractActionForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    SessionContainer sessionCon = this.getSessionContainer(request);
    Connection conn = this.getConnection(request);
    MaintStaffRecordForm staffRecordForm = (MaintStaffRecordForm)form;
    UserRecordDAObject userRecordDAO = new UserRecordDAObject(sessionCon, conn);
    UserRecord userRecord = (UserRecord)userRecordDAO.getObjectByUserTypeUserLinkID(UserRecord.USER_TYPE_STAFF, new Integer(form.getID()));
    
    staffRecordForm.setLevel1ID(request.getParameter("level1ID"));
    staffRecordForm.setLevel2ID(request.getParameter("level2ID"));
    staffRecordForm.setLevel3ID(request.getParameter("level3ID"));
    staffRecordForm.setLevel4ID(request.getParameter("level4ID"));
    staffRecordForm.setLevel5ID(request.getParameter("level5ID"));
    staffRecordForm.setLevel6ID(request.getParameter("level6ID"));
    staffRecordForm.setLevel7ID(request.getParameter("level7ID"));
    staffRecordForm.setLevel8ID(request.getParameter("level8ID"));
    staffRecordForm.setLevel9ID(request.getParameter("level9ID"));
    staffRecordForm.setLevel10ID(request.getParameter("level10ID"));
    
    if (userRecord != null) {
      userRecord.setFirstName(staffRecordForm.getFirstName());
      userRecord.setLastName(staffRecordForm.getLastName());
      userRecord.setFullName(staffRecordForm.getFullName());
      userRecord.setEmail(staffRecordForm.getEmail());
      userRecord.setLoginPwd(null);
      userRecord = (UserRecord)userRecordDAO.updateObject(userRecord);
      // Update UserInfoFactory entry.
      UserInfoFactory.updateUserInfo(userRecord.getID(), new String[]{userRecord.getFullName(), userRecord.getEmail()});
    }
    super.updateRecord(mapping, form, request, response);
  }
  
  public void deleteRecord(ActionMapping mapping, AbstractActionForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    MaintStaffRecordForm recordForm = (MaintStaffRecordForm)form;
    Integer staffID = new Integer(recordForm.getID());
    if (recordForm.getDeleteUserRecord().equals("true")) {
      StaffRecordDAObject  dao = (StaffRecordDAObject) this.getMajorDAObject(request);
      dao.deleteRelatedUserRecord(staffID);
    }
    super.deleteRecord(mapping, form, request, response);
  }
}
